#!/bin/csh
# THIS SUB-SCRIPT IS USED TO MODIFY THE DATA-STATUS FILE.
# NEED setenv datadir
# usage: ./modify-status.csh gfs 2010010112 1
# usage: ./modify-status.csh gfs 2010010112 init
# usage: ./modify-status.csh gfs 2010010112 time

set jobname = $1
set runtime  = $2
set action   = $3

set year = `printf $runtime | cut -c1-4`
if ( ! -d $datadir/data-status/$year/ ) then
    mkdir -p $datadir/data-status/$year/
endif
set target_file = "$datadir/data-status/$year/data-status.${jobname}.${runtime}.txt"
set tmp_file    = "$datadir/data-status/data-status.${jobname}.$runtime.tmp"

if ( $action == "init" ) then
    touch $target_file
    cat $target_file | awk '$1!='"$runtime"'{print}' > $tmp_file
    printf "$runtime" >> $tmp_file
    @ i = 0
    while ( $i < 1 )
        printf "\t0" >> $tmp_file
        @ i = $i + 1
    end
    printf "\n" >> $tmp_file
    cat $tmp_file | sort > $target_file
else
    set value    = ${action}
    if ( $action == "time" ) then
        cat $target_file | awk "/${runtime}/"'{OFS="\t";$1='${value}'}{print}' > $tmp_file
    else
        cat $target_file | awk "/${runtime}/"'{OFS="\t";$2='${value}'}{print}' > $tmp_file
    endif
    mv -f $tmp_file $target_file
endif

